package de.tudarmstadt.ukp.muip.fibqdojo.util;

import java.util.LinkedList;
import java.util.List;

import de.tudarmstadt.ukp.muip.fibqdojo.interfaces.IFIBQSentence;
import de.tudarmstadt.ukp.muip.fibqdojo.interfaces.IFIBQToken;
import de.tudarmstadt.ukp.muip.fibqdojo.visitor.GetTokenListVisitor;

/**
 * Splits a provided sentence into NGrams of specified size
 * 
 * @author Tobias Horsmann
 */
public class NGramConverter {

    private final int ngramSize;

    public NGramConverter(int ngramSize) {
        validateSize(ngramSize);
        this.ngramSize = ngramSize;
    }

    private void validateSize(int size) {
        if (size <= 0) {
            throw new IllegalArgumentException("Size must be >= 1");
        }
    }

    public List<NGram> convert(IFIBQSentence sentence, String separator) {

        GetTokenListVisitor visitor = new GetTokenListVisitor();
        sentence.accept(visitor);
        List<IFIBQToken> tokens = visitor.getTokens();

        List<NGram> threeGrams = new LinkedList<NGram>();

        int end = tokens.size() - ngramSize;
        for (int i = 0; i <= end; i++) {
            NGram ngram = new NGram(ngramSize, separator, tokens.subList(i, i + ngramSize));
            ngram.setSeparator(separator);
            threeGrams.add(ngram);
        }

        return threeGrams;
    }

}
